<template>
  <div
    class="w-[100%] bg-[#fff] rounded-[10px] wrapper"
    @click="onClick"
    data-tracker="offer"
    :data-aplus-report="goods.traceInfo"
    @mouseenter="onHover"
  >
    <div class="cover-wrapper w-full relative pb-[100%] overflow-hidden text-[0]">
      <img
        class="absolute top-0 left-0 object-cover w-full h-full"
        :src="goods.imageUrl"
      />

      <n-popover class="black-popover" trigger="hover" placement="bottom" raw>
        <template #trigger>
          <div class="float-wrapper" @click.stop="onClickSimilar">
            <n-spin v-if="uploading" size="small"> </n-spin>
            <n-icon v-else :size="24" class="pl-[2px]">
              <ImageSearchAlt></ImageSearchAlt>
            </n-icon>
          </div>
        </template>
        <div class="tips">
          {{ $t("tips.以图搜图") }}
        </div>
      </n-popover>
      <div class="recommend-tip" @click.stop="onClickRecommend">
        <div class="top flex justify-center items-center" @click.stop="onClickAddCompare">
          <div v-if="loading" class="relative">
            <n-spin :size="14" stroke="#fff"></n-spin>
          </div>
          <n-icon v-else :size="20" class="pl-[2px]">
            <Compare></Compare>
          </n-icon>
          <span class="ml-2">{{
            isAdded ? $t("button.已加入比较") : $t("button.加入比较")
          }}</span>
        </div>
        <!-- <div class="flex items-center justify-center button-wrapper">
          <div class="" @click.stop="onClickRecommend">
            {{ $t("button.同类产品") }}
          </div>
          <div class="" @click.stop="onClickSimilar">相似</div>
        </div> -->
      </div>
    </div>

    <div class="p-[12px] pt-0">
      <div class="mt-[8px] h-[68px] text-[14px] font-[300] color-[#303030]">
        <n-ellipsis style="width: 100%" :tooltip="false" :line-clamp="3">
          <n-tag
            size="small"
            type="success"
            class="rounded-[4px] tags"
            v-if="goods.isJxhy"
          >
            {{ $t("common.精选") }}
          </n-tag>
          {{ goods.subjectTrans }}
        </n-ellipsis>
      </div>

      <div class="text-[14px] text-[#707070] price-wrapper">
        <div class="flex-1 width-0">
          <money
            :money="goods.priceInfo?.price"
            :moneyStyle="{ color: 'var(--main-color)' }"
          ></money>
          <!-- <span class="text-[#707070]">¥</span>
        <span class="text-[20px] text-[#EA0000] ml-[4px]">{{
          goods.priceInfo?.price
        }}</span> -->
        </div>
        <div class="flex items-center">
          <div class="flex-1">
            <money
              :money="goods.priceInfo?.price"
              :is-cny="false"
              :moneyStyle="{
                fontSize: '14px',
                fontWeight: '400',
                color: 'var(--main-color)',
              }"
            ></money>
            <!-- <span class="text-[#707070]">$</span>
          <span class="text-[20px] text-[#EA0000] ml-[4px]">
            {{ (goods.priceInfo?.price * 2.5).toFixed(2) }}
          </span> -->
          </div>
          <div class="text-[--sub-color]" v-if="goods.monthSold">
            {{ formatMoney(goods.monthSold, 0) }} {{ $t("common.销量") }}
          </div>
        </div>
        <!-- <div
          :style="{ right: loading ? '0' : '' }"
          class="compare-wrapper"
          @click.stop="onClickAddCompare"
        >
          <n-spin v-if="loading" size="small"></n-spin>
          <span class="ml-2">{{ $t("button.比较") }}</span>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps } from "vue";
// import { useRouter } from "vue-router";
import money from "@/components/money.vue";
import { formatMoney } from "@/utils/formatMoney";
import { goGoods } from "@/utils/common";
import { useCompare } from "@/stores/compare";
import { imageUrlSearch } from "@/utils/common";
import { ImageSearchAlt, Compare } from "@vicons/carbon";
const loading = ref(false);
const uploading = ref(false);
const isAdded = ref(false);
const { addCompareGoods, isAdd } = useCompare();
// const router = useRouter();
const props = defineProps({
  goods: {},
});

const onClick = () => {
  goGoods(props.goods.offerId);
};

const onClickRecommend = () => {
  window.open(`/#/recommend/${props.goods.offerId}`, "_blank");
};

const onClickAddCompare = async () => {
  if (loading.value) return;
  loading.value = true;
  await addCompareGoods(props.goods.offerId);
  loading.value = false;
  onHover();
};

const onClickSimilar = async () => {
  uploading.value = true;
  await imageUrlSearch(props.goods.imageUrl);
  uploading.value = false;
};

const onHover = () => {
  isAdded.value = isAdd(props.goods.offerId);
};
</script>

<style lang="scss" scoped>
.wrapper {
  cursor: pointer;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
  img {
    transition: all 0.3s ease-in-out;
  }
  .compare-wrapper {
    position: absolute;
    background-color: var(--sub-color);
    color: white;
    right: -160px;
    bottom: 13px;
    width: 160px;
    height: 46px;
    line-height: 46px;
    text-align: center;
    font-size: 18px;
    font-weight: 300;
    transition: all 0.3s ease-in-out;
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &:hover {
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .compare-wrapper {
      right: 0px;
    }
    img {
      transform: scale(1.1);
    }
    .recommend-tip {
      bottom: 0;
    }
  }
}

.cover-wrapper {
  .tips {
    background-color: #000;
  }
  .recommend-tip {
    position: absolute;
    bottom: -100px;
    width: 100%;
    // padding: 8px 0;
    // height: 24px;
    line-height: 24px;
    z-index: 1;
    font-size: 14px;
    text-align: center;
    background-color: #fff;
    transition: all 0.3s ease-in-out;
    color: #303030;
    .top {
      background-color: #fff4e4;
      color: var(--main-color);
      padding: 6px 0;
      transition: all 0.3s ease-in-out;
      &:hover {
        background-color: var(--main-color);
        color: white;
      }
    }
    .button-wrapper {
      border-bottom: 1px solid #eeeeee;
      margin: 0 12px;
      padding: 6px 0;
      > div {
        flex: 1;
      }
      > div:last-child {
        border-left: 1px solid #eeeeee;
      }
    }
  }
  // &:hover {
  //   .recommend-tip {
  //     bottom: 0;
  //   }
  // }
}
</style>
